package cn.edu.zju.om.nlp;

import java.util.List;
import java.util.Properties;

import edu.stanford.nlp.ie.crf.CRFClassifier;

public class SUWordSegmentorAdapter implements WordSegmentor {
	
	private static CRFClassifier classifier;
	
	public SUWordSegmentorAdapter() {
		if (classifier == null) {
		    loadClassifier();
		}
	}

	private static void loadClassifier() {
		Properties props = new Properties();
		props.setProperty("sighanCorporaDict", "data");
		// props.setProperty("NormalizationTable", "data/norm.simp.utf8");
		// props.setProperty("normTableEncoding", "UTF-8");
		// below is needed because CTBSegDocumentIteratorFactory accesses it
		props.setProperty("serDictionary","data/dict-chris6.ser.gz");

		props.setProperty("inputEncoding", "UTF-8");
		props.setProperty("sighanPostProcessing", "true");
		
		classifier = new CRFClassifier(props);
		classifier.loadClassifierNoExceptions("data/ctb.gz", props);
		// flags must be re-set after data is loaded
		classifier.flags.setProperties(props);
	}

	public List<String> segment(String sentence) {
		return classifier.segmentString(sentence);
	}

}
